package cn.pli.femedia.MPEG;

import android.media.MediaExtractor;
import android.media.MediaFormat;

/**
 * Created by shangdongzhou on 2017/5/24.
 * 读取输入文件的MP4信息
 */

public class GetMP4Info {

    /**
     * Selects the video track, if any.
     * @return the track index, or -1 if no video track is found.
     */
    public static int selectAudioTrack(MediaExtractor extractor) {
        int numTracks = extractor.getTrackCount();
        for (int i = 0; i < numTracks; i++) {
            MediaFormat format = extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);       //format 的类型
            if (mime.startsWith("audio/")) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Selects the video track, if any.
     * @return the track index, or -1 if no video track is found.
     */
    public static int selectVideoTrack(MediaExtractor extractor) {
        int numTracks = extractor.getTrackCount();
        for (int i = 0; i < numTracks; i++) {
            MediaFormat format = extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);       //format 的类型
            if (mime.startsWith("video/")) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 获取视频的Fromat
     * @param extractor
     * @return
     */
    public static MediaFormat getVideoFormat(MediaExtractor extractor) {
        MediaFormat format=null;
        int numTracks = extractor.getTrackCount();
        for (int i = 0; i < numTracks; i++) {
             format = extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);       //format 的类型
            if (mime.startsWith("video/")) {
                return format;
            }
        }
        return format;
    }

    /**
     * 获取音频的Fromat
     * @param extractor
     * @return
     */
    public static MediaFormat getVoiceFormat(MediaExtractor extractor) {
        MediaFormat format=null;
        int numTracks = extractor.getTrackCount();
        for (int i = 0; i < numTracks; i++) {
            format = extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);       //format 的类型
            if (mime.startsWith("audio/")) {
                return format;
            }
        }
        return format;
    }

    /**
     * 获取视频大小
     * @param extractor
     * @return
     */
    public static Size getVideoSize(MediaExtractor extractor){
        Size size=new Size(-1,-1);
        MediaFormat format = getVideoFormat(extractor);
        size.setmWidth(format.getInteger(MediaFormat.KEY_WIDTH));
        size.setmHeight(format.getInteger(MediaFormat.KEY_HEIGHT));
        return size;
    }


}
